<?php
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class UserMonthly extends Common
{
    /**
     * 用过user访问user child
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['u.username', 'like', '%' . trim($post['username']) . '%'];
        }
        if(isset($post['user_type']) && strlen($post['user_type']) > 0){
            $where[] = ['u.user_type', '=', $post['user_type']];
        }
        if(isset($post['month_user']) && strlen($post['month_user']) > 0){
            if($post['month_user'] == 1) {
                $where[] = ['user_id', 'not in', '2,3,22,120,123'];
            }else if($post['month_user'] == 2){
                //$where[] = ['card_total', '=', 0];
                $where[] = ['user_id', 'in', '2,3,22,120,123'];
            }
        }

        $list = Db::name('user_monthly')->alias('um')
                ->leftJoin("user u", "u.id=um.user_id")
                ->field('um.* , u.username,u.user_type,u.month_fee')
                ->where($where)
                ->order('um.id desc')
                ->paginate(['list_rows' => $list_rows, 'page' => $page])
                ->toArray();
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    public function updateMonthly()
    {
        $user_list = Db::name('user')->field('id')->select()->toArray();
        $time      = getUtcTime();
        $blog      = true;
        Db::startTrans();
        try{
            Db::name('user_monthly')->where('id > 0')->delete();
            foreach($user_list as $v){
                $user_id        = $v['id'];
                $total_count    = Db::name('cards')->where(['user_id' => $user_id])->count();
                $active_count   = Db::name('cards')->where(['user_id' => $user_id , 'status' => 'ACTIVE'])->count();
                $sup_count      = Db::name('cards')->where(['user_id' => $user_id , 'status' => 'SUSPENDED'])->count();
                $del_count      = Db::name('cards')->where(['user_id' => $user_id , 'status' => 'TERMINATED'])->count();
                if($total_count >= 1000){
                    Db::name('user')->where(['id' => $user_id])->update(['month_fee' => 149]);
                }
                Db::name('user_monthly')->save([
                    'user_id'       => $user_id,
                    'card_total'    => $total_count,
                    'active'        => $active_count,
                    'suspended'     => $sup_count,
                    'cancel'        => $del_count,
                    'ctime'         => $time
                ]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $blog = false;
        }
        return [
            'blog'   =>  $blog,
            'time'   =>  date('Y-m-d H:i:s',$time + 8*60*60),
        ];
    }

    public function checkUserFee($user_id)
    {
        //获取本月的开始时间和结束时间
        $time       = getUtcMoneyTime();
        $start_time = $time['start_time'];
        $end_time   = $time['end_time'];
        //判断本月是否已收费过
        return Db::name('user_fee')
            ->where("user_id={$user_id} and (ctime between {$start_time} and {$end_time} )")
            ->find();
    }

    /**
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataMonthDebt($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];

        $where[] = ['debt', '>', '0'];

        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['username', 'like', '%' . trim($post['username']) . '%'];
        }
        if(isset($post['business_token']) && !empty($post['business_token'])){
            $where[] = ['business_token', 'like', '%' . trim($post['business_token']) . '%'];
        }
        if(isset($post['user_memo']) && !empty($post['user_memo'])){
            $where[] = ['user_num', 'like', '%' . trim($post['user_memo']) . '%'];
        }
        if(isset($post['user_type']) && strlen($post['user_type']) > 0){
            $where[] = ['user_type', 'like', $post['user_type']];
        }
        $list = Db::name('user')
            ->where($where)
            ->order('check_time desc')
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}